/*****************************************************************************
Replication File for Asian American Mobilization: The Effect of Candidates 
and Districts on Asian American Turnout

Sara Sadhwani
Pomona College
****************************************************************************/


clear

* You will need to replace with your own file path.

capture: log close
pause off
set more off


use "datset_asiantrn_cagn12-18.dta"


////////////////////////////INDEPENDENT VARIABLES\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
destring cand1_racecode, replace
destring cand2_racecode, replace
destring cand1_asiancode, replace
destring cand2_asiancode, replace

destring margin, replace
destring cand2_pid, replace
destring cand2_gender, replace
destring cand2_incumbent, replace
destring election_result_cand2, replace

*Gen asian cand dummy
gen asiancand=0
replace asiancand=1 if cand1_racecode==4 | cand2_racecode==4
replace asiancand = . if missing(cand1_racecode) & missing(cand2_racecode) 
label var asiancand "Co-Ethnic Candidate"


*Gen dummy variables for Chinese Candidates (Includes Taiwanese)
gen chicand=0
replace chicand=1 if (cand1_asiancode==1 | cand1_asiancode==7) | (cand2_asiancode==1 | cand2_asiancode==7)
replace chicand= . if missing(cand1_asiancode) & missing(cand2_asiancode)
label var chicand "Chinese Candidate"


*Gen dummy variable for Indian Candidates *
gen indcand=0
replace indcand=1 if cand1_asiancode==4 | cand2_asiancode==4
replace indcand = . if missing(cand1_asiancode) & missing(cand2_asiancode) 
label var indcand "Indian candidate"

*Gen dummy variable for Japanese Candidates
gen jpncand=0
replace jpncand=1 if cand1_asiancode==2 | cand2_asiancode==2
replace jpncand = . if missing(cand1_asiancode) & missing(cand2_asiancode) 
label var jpncand "Japanese Candidate"

*Gen dummy variable for Korean Candidates
gen korcand=0
replace korcand=1 if cand1_asiancode==3 |cand2_asiancode==3
replace korcand = . if missing(cand1_asiancode) & missing(cand2_asiancode) 
label var korcand "Korean Candidate"

*Gen dummy variable for Vietnamese Candidates
gen vietcand=0
replace vietcand=1 if cand1_asiancode==5 | cand2_asiancode==5
replace vietcand = . if missing(cand1_asiancode) & missing(cand2_asiancode) 
label var vietcand "Vietnamese Candidate"

*Gen dummy variable for Filipino Candidates
gen filcand=0
replace filcand=1 if cand1_asiancode==6 | cand2_asiancode==6
replace filcand = . if missing(cand1_asiancode) & missing(cand2_asiancode) 
label var filcand "Filipino Candidate"


////////////////////////////CONTROLS\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

*Open Seat
label var openseat "Open Seat"

*Partisan Election 
gen partisan=0
destring cand2_pid, replace
replace partisan=1 if cand1_pid==cand2_pid
label var partisan "Partisan Election"

*Competitiveness: Margin of Victory  
gen competitive=0
destring margin, replace
replace competitive=1 if margin<=5
replace competitive=2 if margin>5 & margin<=10
replace competitive=3 if margin>10 & margin<=20
replace competitive=4 if margin>20
replace competitive=. if missing(margin)
label var competitive "Competitiveness"  

* Dummy Variables for margin
quietly tab competitive, gen(comp_)
label var comp_4 "Margin of victory >20%"
label var comp_3 "Margin of victory between 10-20%"
label var comp_2 "Margin of victory between 5-10%"
label var comp_1 "Margin of victory <5%"

*Dummy Variable for Candidate Gender
destring cand1_gender, replace
destring cand2_gender, replace
gen female=0
replace female=1 if cand1_gender==1 | cand2_gender==1
replace female = . if missing(cand1_gender) & missing(cand2_gender)
label var female "Female"

*Candidate PID
gen cand_pid=0
replace cand_pid=1 if cand1_pid==2 /*Dem*/
replace cand_pid=2 if cand1_pid==1 /*Rep*/
replace cand_pid=3 if cand1_pid==3 | cand1_pid==4 | cand1_pid==5
 *1=rep, 2=dem, 3=NPP, 4=Lib, 5=AI		

*Election Year
 encode election, gen(yr)
replace yr = yr-1
label define year 0 "2012" 1 "2014" 2 "2016" 3 "2018"
label val yr year

* Define Labels
label var asiantrn "Asian Turnout"
label var asiancand "Asian Candidate"

label define asiancand 0 "No Asian Candidate" 1 "Asian Candidate"
label val asiancand asiancand

label define openseat 0 "No Open Seat" 1 "Open Seat"
label val openseat openseat

label define partisan 0 "Not-partisan Election" 1 "Partisan Election"
label val partisan partisan

label define female 0 "Male" 1 "Female"
label val female female



////////////////////////////////MODELS\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

 //GENERALIZED ESTIMATING EQUATIONS \\
xtset addist 
set matsize 2000


**Regression Table A: Asian Am Turnout, CA Assembly Elections 2012-2018
eststo clear
xtgee asiantrn i.asiancand c.pct_asian i.asiancand#c.pct_asian i.openseat i.comp_1 i.comp_2 i.comp_3 i.yr i.female i.partisan, robust 
eststo
esttab using table_asian.tex, se replace nonumbers label ///
	title (Asian American Turnout with Pan-Ethnic Candidates in California Assembly Elections 2012-2018) ///
	mtitles("Total Asian Turnout")


**National Origin Turnout: Regession Tables B & C and Panels in Figure 5
eststo clear

  *chinese turnout
xtgee chitrn i.chicand c.pct_chi i.chicand#c.pct_chi i.openseat i.comp_1 i.comp_2 i.comp_3 i.yr i.female i.partisan, robust
eststo
margins chicand, at(pct_chi=(0(.20).40)) 
marginsplot

  **indian turout
xtgee indtrn i.indcand c.pct_ind i.indcand#c.pct_ind i.openseat i.comp_0 i.comp_1 i.comp_2 i.comp_3 i.comp_4 i.yr i.female i.partisan, robust
xtgee indtrn i.indcand c.pct_ind i.indcand#c.pct_ind i.openseat i.comp_1 i.comp_2 i.comp_3 i.comp_4 yr2014 yr2016 i.female i.partisan, robust
eststo
margins indcand, at(pct_ind=(0(.08).16)) 
marginsplot

  **filipino turnout
xtgee filtrn i.filcand c.pct_fil i.filcand#c.pct_fil i.openseat i.comp_1 i.comp_2 i.comp_3 i.comp_4 yr2014 yr2016 i.female i.partisan, robust
eststo
margins filcand, at(pct_fil=(0(.06).12)) 
marginsplot

  **regression table b: Chinese, Indian and Filipino Turnout with Co-Ethnic Candidates 
esttab using table_gee-chi-ind-fil.tex, se replace nonumbers label ///
	title (Chinese, Indian and Filipino American Turnout with National Origin Co-Ethnic Candidates, California Assembly Elections 2012-2016) ///
	mtitles("Chinese Turnout" "Indian Turnout" "Filipino Turnout")
	

**Table 3: KOR-JPN & Figure 5
eststo clear
  *korean turnout model
xtgee kortrn i.korcand c.pct_kor i.korcand#c.pct_kor i.openseat comp_1 comp_2 comp_3 comp_4 yr2012 yr2014 yr2016 i.female i.partisan, robust
eststo
margins korcand, at(pct_kor=(0(.06).12)) 
marginsplot

  *japanese turnout model
xtgee jpntrn i.jpncand c.pct_jpn i.jpncand#c.pct_jpn i.openseat comp_1 comp_2 comp_3 comp_4 yr2012 yr2014 yr2016 i.female i.partisan, robust
eststo
margins jpncand, at(pct_jpn=(0(.03).06)) 
marginsplot

  **regression table c: Korean and Japanese Turnout with Co-Ethnic Candidates
esttab using table_gee-kor-jpn.tex, se replace nonumbers label ///
	title (Korean and Japanese American Turnout with National Origin Co-Ethnic Candidates, California Assembly Elections 2012-2016) ///
	mtitles("Korean Turnout" "Japanese Turnout")
	
	

//////////////////ANALYSIS OF PARTISANSHIP ~ APPENDIX B \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\

*Figure 4: Predicted Asian American Turnout by Partisanship
***Full results of the models are available in Appendix B. 

  *independent variables
*Asian Democrat Voter Turnout
gen asiandemvote=vote_kordem + vote_jpndem + vote_chidem + vote_inddem + vote_vietdem + vote_fildem
gen asiandemreg= reg_kordem + reg_jpndem + reg_chidem + reg_inddem + reg_vietdem + reg_fildem
	gen asiandemtrn=asiandemvote/asiandemreg

*Asian Republican Voter Turnout
gen asianrepvote= vote_korrep + vote_jpnrep + vote_chirep + vote_indrep + vote_vietrep + vote_filrep
gen asianrepreg= reg_korrep + reg_jpnrep + reg_chirep + reg_indrep + reg_vietrep + reg_filrep
	gen asianreptrn= asianrepvote/asianrepreg

*Asian Decline to State/Independent Turnout
gen asiandclvote= vote_kordcl + vote_jpndcl + vote_chidcl + vote_inddcl + vote_vietdcl + vote_fildcl
gen asiandclreg= reg_kordcl + reg_jpndcl + reg_chidcl + reg_inddcl + reg_vietdcl + reg_fildcl
	gen asiandcltrn=asiandclvote/asiandclreg

	
	**dependent variables
*Asian Democrat Candidate
gen asiandemcand=0
	replace asiandemcand=1 if asiancand==1 & (cand1_pid==2 | cand2_pid==2)

*Asian Republican Candidate
gen asianrepcand=0
	replace asianrepcand=1 if asiancand==1 & (cand1_pid==1 | cand2_pid==1)


	**MODELS**
*asian trnout by partisanship w/ asian dem cand
reg asiandemtrn i.asiandemcand incumbent i.openseat i.comp_1 i.comp_2 i.comp_3 i.yr i.female i.partisan
margins asiandemcand

reg asianreptrn i.asiandemcand incumbent i.openseat i.comp_1 i.comp_2 i.comp_3 i.yr i.female i.partisan
margins asiandemcand

reg asiandcltrn i.asiandemcand incumbent i.openseat i.comp_1 i.comp_2 i.comp_3 i.yr i.female i.partisan
margins asiandemcand

*asian trnout by partisanship w/ asian rep cand
reg asiandemtrn i.asianrepcand incumbent i.openseat i.comp_1 i.comp_2 i.comp_3 i.yr i.female i.partisan
margins asianrepcand

reg asianreptrn i.asianrepcand incumbent i.openseat i.comp_1 i.comp_2 i.comp_3 i.yr i.female i.partisan
margins asianrepcand

reg asiandcltrn i.asianrepcand incumbent i.openseat i.comp_1 i.comp_2 i.comp_3 i.yr i.female i.partisan
margins asianrepcand
